package cn.exrick.xboot.modules.member.serviceimpl;

import cn.exrick.xboot.modules.member.dao.MemberDao;
import cn.exrick.xboot.modules.member.entity.Member;
import cn.exrick.xboot.modules.member.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;

import java.text.SimpleDateFormat;
import java.util.List;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.function.Consumer;

/**
 * 会员用户接口实现
 * @author Ydp
 */
@Slf4j
@Service
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public MemberDao getRepository() {
        return memberDao;
    }

    @Override
    public Page<Member> findByCondition(Member member, SearchVo searchVo, Pageable pageable) {

        Page<Member>  pages =  memberDao.findAll(new Specification<Member>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<Member> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<Date> createTimeField = root.get("createTime");
                Path<String> memberCardNoField = root.get("memberCardNo");
                Path<String> memberNameField = root.get("memberName");
                Path<String> memberTelField = root.get("memberTel");


                List<Predicate> list = new ArrayList<>();

                // 创建时间
                if(StrUtil.isNotBlank(searchVo.getStartDate())&&StrUtil.isNotBlank(searchVo.getEndDate())){
                    Date start = DateUtil.parse(searchVo.getStartDate());
                    Date end = DateUtil.parse(searchVo.getEndDate());
                    list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
                }
                if (StringUtils.isNotBlank(member.getMemberCardNo())){
                    list.add(cb.like(memberCardNoField,"%" + member.getMemberCardNo() + "%"));
                }
                if (StringUtils.isNotBlank(member.getMemberName())) {
                    list.add(cb.like(memberNameField,"%" + member.getMemberName() + "%"));
                }
                if (StringUtils.isNotBlank(member.getMemberTel())) {
                    list.add(cb.like(memberTelField,"%" + member.getMemberTel() + "%"));
                }

                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);

        pages.getContent().forEach(member1 -> {
            Date startTime =  DateUtil.parseDate(new SimpleDateFormat("yyyy-MM-dd").format(member1.getMemberUseDate()));
            Date endTime =   DateUtil.parseDate(new SimpleDateFormat("yyyy-MM-dd").format(member1.getMemberEndDate()));
            Date nowDate = DateUtil.parseDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            long count = DateUtil.betweenDay(nowDate,endTime,true);
            if (startTime.compareTo(nowDate) > 0){
                member1.setMemeberTimeStatus(-1);
                member1.setMemeberTimeStatusStr("未到开卡日期");
            }else if (nowDate.compareTo(endTime) > 0){
                member1.setMemeberTimeStatus(0);
                member1.setMemeberTimeStatusStr("过期【"+count+"】天");
            } else {
                if (count <31L){
                    member1.setMemeberTimeStatus(1);
                    member1.setMemeberTimeStatusStr("剩余【"+count+"】天");
                } else {
                    member1.setMemeberTimeStatus(2);
                    member1.setMemeberTimeStatusStr("剩余【"+count+"】天");
                }
            }



        });

        return  pages;
    }


    public static void main(String[] args) {

        //0
     long count = DateUtil.betweenDay(DateUtil.parseDate("2023-01-20"),DateUtil.parseDate("2023-01-19"),true);
        System.out.println(count);



    }

}